/*
 *  $Id: Notice.cs 574 2009-06-25 10:38:55Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    public class BattleNotice {
        private const int MaxShow = 4;

        private List<string> _data;
        private string _new;
        private int _step, _time, _newTime;
        private bool _show;

        public BattleNotice() {
            _data = new List<string>();
            _new = null;
            _step = 0;
            _time = 0;
            _newTime = 0;
            _show = false;
        }

        public bool Visible {
            get { return _step > 0; }
        }

        public int Time {
            get { return _time; }
        }

        public void Show(string text) { Show(text, 0); }
        public void Show(string text, int time) {
            if (_show) Hide();
            _new = text;
            _newTime = time;
        }

        public void Hide() {
            _show = false;
        }

        private void Update(string text) {
            _data.Clear();
            if (string.IsNullOrEmpty(text)) return;
            _data.AddRange(Data.Fonts.Main.Wrap(text, Constants.NoticeTextWidth));
        }
 
        public void Draw() {
            if (_step == 0) return;
            double alpha = (double) _step / MaxShow;
            Data.BattleNotice.Alpha = alpha;
            Data.BattleNotice.Draw(Constants.NoticeX, Constants.NoticeY);
            if (_data.Count == 0) return;
            Data.Fonts.Notice.Color = Color.Black;
            Data.Fonts.Notice.Alpha = alpha;
            int x = Constants.NoticeX + Constants.NoticeWidth/2;
            int y = Constants.NoticeY + (Constants.NoticeHeight - _data.Count*Constants.NoticeRowHeight)/2;
            foreach (string s in _data) {
                Data.Fonts.Notice.DrawCenter(x, y, s);
                y += Constants.NoticeRowHeight;
            }
        }

        public void Process() {
            if (_show)
                ProcessShow();
            else
                ProcessHide();
        }

        private void ProcessShow() {
            if (_step < MaxShow)
                _step++;
            else if (_time > 0) {
                _time--;
                if (_time == 0) Hide();
            }
        }

        private void ProcessHide() {
            if (_step > 0) _step--;
            if (_step == 0 && !string.IsNullOrEmpty(_new)) {
                Update(_new);
                _time = _newTime;
                _new = null;
                _newTime = 0;
                _show = true;
            }
        }
    }
}